<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 论坛推荐接口
 */
class Forumrecommend extends Api
{

    protected $noNeedRight = ['*'];

    //推荐/不推荐
    public function add()
    {
        $forum_id = input('forum_id',0);
        if (empty($forum_id)) {
            $this->error('缺少参数论坛id');
        }

        $recommend_type = input('recommend_type',0);

        $info = \app\api\model\forum\ForumRecommend::where(['user_id'=>$this->auth->id,'forum_id'=>$forum_id])->find();
        if ($info){
            if ($recommend_type==$info->recommend_type){
                $info->delete();
                if ($recommend_type==0){
                    \app\api\model\forum\Forum::where(['forum_id'=>$forum_id])->setDec('not_recommend_num',1);
                } else {
                    \app\api\model\forum\Forum::where(['forum_id'=>$forum_id])->setDec('recommend_num',1);
                }
            } else {
                if ($recommend_type==0){
                    \app\api\model\forum\Forum::where(['forum_id'=>$forum_id])->setDec('recommend_num',1);
                    \app\api\model\forum\Forum::where(['forum_id'=>$forum_id])->setInc('not_recommend_num',1);
                } else {
                    \app\api\model\forum\Forum::where(['forum_id'=>$forum_id])->setDec('not_recommend_num',1);
                    \app\api\model\forum\Forum::where(['forum_id'=>$forum_id])->setInc('recommend_num',1);
                }
                $info->recommend_type = $recommend_type;
                $info->save();
            }
        } else {
            $model = new \app\api\model\forum\ForumRecommend();

            $data = [
                'user_id'=>$this->auth->id,
                'forum_id'=>$forum_id,
                'recommend_type'=>$recommend_type
            ];
            if ($recommend_type==0){
                \app\api\model\forum\Forum::where(['forum_id'=>$forum_id])->setInc('not_recommend_num',1);
            } else {
                \app\api\model\forum\Forum::where(['forum_id'=>$forum_id])->setInc('recommend_num',1);
            }
            $res = $model->save($data);
        }

        $this->success('请求成功');
    }

    //是否点过推荐
    public function isrecommend()
    {
        $forum_id = input('forum_id',0);
        if (empty($forum_id)) {
            $this->error('缺少参数论坛id');
        }

        $info = \app\api\model\forum\ForumRecommend::where(['forum_id'=>$forum_id, 'user_id' => $this->auth->id])->find();

        if (!empty($info)) {
            $this->success('',$info['recommend_type']);
        } else {
            $this->success('',null);
        }
    }

}